//
//  myCoach.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "myCoach.h"
#import "studentDetialView.h"
#import "contentView.h"
#import "startView.h"
@interface myCoach () {
    studentDetialView *headView;
    contentView *contentV;
    NSString *lenderId;
    __block NSNumber *evaluationStar;
    UIScrollView *scrollview;
}

@end

@implementation myCoach

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的教练";
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin);
    scrollview = [[UIScrollView alloc]initWithFrame:frame];
    scrollview.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:scrollview];
    if (@available(iOS 11.0, *)) {
        scrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    headView = [[studentDetialView alloc]initWithFrame:CGRectMake(0, 1, kScreenWidth, 100)];
    [scrollview addSubview:headView];
    [self detialRequest];
}

#pragma mark --- 请求我的教练详情页数据
- (void)detialRequest {
    NSDictionary *dic = @{
                          @"token":USERTOKEN
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] lenderDetial], self.ID];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf updateUIWithDic:responseObject[@"data"]];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)updateUIWithDic:(NSDictionary *)data {
    lenderId = [NSString stringWithFormat:@"%@", data[@"id"]];
    headView.phone = data[@"lenderPhone"];
    headView.name_lab.text = data[@"lenderName"];
    headView.age_lab.text = [NSString stringWithFormat:@"%@", data[@"car_age"]];
    if ([data[@"lenderSex"] isEqualToString:@"男"]) {
        
    }else {
        headView.sex_lab.text = @"♀";
    }
    headView.success_lab.text = @"";
    [headView.head_ImagV sd_setImageWithURL:[NSURL URLWithString:data[@""]] placeholderImage:[UIImage imageNamed:@"headPortrait"]];
    [headView.starView setNumber:data[@"start"]];
    headView.starView.userInteractionEnabled = NO;
    [self addEvaluationViewWithStart:data[@"apprFlg"] andData:data];
}

- (void)addEvaluationViewWithStart:(NSString *)flag andData:(NSDictionary *)dic{
    if ([flag isEqualToString:@"0"]) {
        contentV = [[NSBundle mainBundle] loadNibNamed:@"contentView"owner:self options:nil].firstObject;
    }else {
        contentV = [[NSBundle mainBundle] loadNibNamed:@"contentView2"owner:self options:nil].firstObject;
    }
    [scrollview addSubview:contentV];
    [contentV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(headView.mas_bottom).offset(10);
        make.height.equalTo(@270);
    }];
    
    startView *_starView = [[startView alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
    _starView.starNumBlock = ^(NSInteger num) {
        evaluationStar = [NSNumber numberWithInteger:num];
    };
    [contentV addSubview:_starView];
    [_starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentV.left_lab.mas_right);
        make.centerY.equalTo(contentV.left_lab.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@15);
    }];
    
    if ([flag isEqualToString:@"0"]) {
        UIButton *comit = [[UIButton alloc]init];
        [comit setTitle:@"提交评价" forState:0];
        comit.backgroundColor = [UIColor custom_color:@"#A91E23"];
        comit.layer.masksToBounds = YES;
        comit.layer.cornerRadius = 22;
        [comit addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchUpInside];
        [scrollview addSubview:comit];
        [comit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(10);
            make.right.equalTo(self.view).offset(-10);
            make.top.equalTo(contentV.mas_bottom).offset(20);
            make.height.equalTo(@50);
        }];
        if (CGRectGetMaxY(comit.frame) + 50 > self.view.frame.size.height) {
            scrollview.contentSize = CGSizeMake(0, CGRectGetMaxY(comit.frame));
        }else {
            scrollview.contentSize = CGSizeMake(0, self.view.frame.size.height + 1);
        }
    }else {
        [_starView setNumber:dic[@"startRcd"]];
        _starView.userInteractionEnabled = NO;
        for (UIView *view in contentV.subviews) {
            if (view.tag == 1010) {
                UITextView *tv = (UITextView *)view;
                tv.text = dic[@"appriRcd"];
            }
        }
    }
}

- (void)commit:(id)sender {
    [self commitRequest];
}
//提交评价
- (void)commitRequest {
    if (evaluationStar == 0) {
        [MBProgressHUD gk_toastShowMessage:@"请评分"];
        return;
    }else if ([contentV.content_tv.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入评价内容"];
        return;
    }
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"score":evaluationStar,
                          @"appraise":contentV.content_tv.text
                          };
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@%@", [[apiManger shareApiManger] commitEvaluation], lenderId];
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
